package guoanmaker.operator.business.controller;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorAppBanner;
import guoanmaker.operator.business.service.OperatorAppBannerServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.*;

/**
 *  
 *
 * @author huZhaoyu 
 * @ClassName: OperatorAppBannerController
 * @Description: OperatorAppBannerController 
 * @date 2017/6/29 上午10:11 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_APP_BANNER)
public class OperatorAppBannerController {
    private final AjaxJson ajaxJson;
    private final OperatorAppBannerServiceInterface operatorAppBannerServiceInterface;

    public OperatorAppBannerController(AjaxJson ajaxJson, OperatorAppBannerServiceInterface operatorAppBannerServiceInterface) {
        this.ajaxJson = ajaxJson;
        this.operatorAppBannerServiceInterface = operatorAppBannerServiceInterface;
    }

    /** 
    * @Title: saveAppBanner
    * @Description: 新增/修改App金刚圈 
    * @param: OperatorAppBanner   
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "保存App金刚圈", notes = "新增/修改App金刚圈")
    @PostMapping(value = "saveAppBanner")
    public AjaxJson saveAppBanner(@RequestBody InRoundPhone inRoundPhone) {
    	try{
    	OperatorAppBanner[] appbans=inRoundPhone.getOperatorAppBanners();
    	for(OperatorAppBanner appban:appbans){
    		if(appban.getId()!=null&&!appban.getId().equals(""))
    		operatorAppBannerServiceInterface.deleteBannerById(appban.getId());
    		appban.setCreateDate(new Date());
    		appban.setCityId(inRoundPhone.getCityId());
    	if(appban.isFasehot()){
    		appban.setIsHot(1);
    	}else{
    		appban.setIsHot(0);
    	}
    	
    	if(appban.isFasenew()){
    		appban.setIsNew(1);
    	}else{
    		appban.setIsNew(0);
    	}
    	if(appban.getCopyitemLevel()!=null&&!appban.getCopyitemLevel().equals("")){
    	if(appban.getCopyitemLevel().equals("1")){
    		appban.setItemLevel(1);
    	}else{
    		appban.setItemLevel(2);
    	}
    	}else{
    		appban.setItemLevel(null);
    	}
    		operatorAppBannerServiceInterface.saveAppBanner(appban);
    	}
    	ajaxJson.setSuccess(true).setStatus(200).setData(null);
    	}catch(Exception e){
    		
    		e.printStackTrace();
    		ajaxJson.setSuccess(true).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

    /** 
     * @Title: deleteAppBannerById
     * @Description: 根据Id删除App金刚圈
     * @param: String id  
     * @return AjaxJson 
     * @throws 
     */
    @ApiOperation(value = "删除App金刚圈", notes = "根据Id删除App金刚圈")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteAppBannerById(@PathVariable(value = "id") String id) {
        operatorAppBannerServiceInterface.deleteAppBannerById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
    }

    /**
     * @return OperatorAppBanner    
     * @Title: getAppBannerById 
     * @Description: 根据Id查询App金刚圈
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询App金刚圈", notes = "根据Id查询App金刚圈")
    @GetMapping(value = "getAppBannerById")
    public AjaxJson getAppBannerById(@RequestParam(value = "id") String id) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorAppBannerServiceInterface.getAppBannerById(id));
    }

   

    /**
     * @return AjaxJson   
     * @Title: getAllAppBannerByPage 
     * @Description: 查询所有App金刚圈并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询App金刚圈", notes = "分页查询App金刚圈")
    @GetMapping(value = "getAllAppBannerByPage")
    public AjaxJson getAllAppBannerByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorAppBannerServiceInterface.getAllAppBannerByPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getAppBannerByPageOrderByWeight 
     * @Description: 按权重排序查询App金刚圈并分页 
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "按权重排序查询App金刚圈并分页", notes = "按权重排序查询App金刚圈并分页")
    @GetMapping(value = "getAppBannerOrderByWeight")
    public AjaxJson getAppBannerByPageOrderByWeight(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorAppBannerServiceInterface.getAppBannerByPageOrderByWeight(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getAll2App 
     * @Description: 查询所有并查询服务名称 
     */
    @ApiOperation(value = "查询所有并查询服务名称", notes = "查询所有并查询服务名称")
    @GetMapping(value = "getAll2App")
    public AjaxJson getAllAndServerName() {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorAppBannerServiceInterface.getAllAndServerName());
    }
    
    @ApiOperation(value = "查找所有App金刚圈", notes = "查找所有App金刚圈")
    @PostMapping(value = "getAllAppBannerPC")
    public AjaxJson getAllAppBannerPC(String cityId) {
    	try{
    	List<OperatorAppBanner> bans=operatorAppBannerServiceInterface.getAllAppBanner(cityId);
    	
    		ajaxJson.setSuccess(true).setStatus(200).setData(bans);
    	
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

}
